package com.ecoolex.hotel.saas.platform.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.hotel.saas.entity.StoreMarketing;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddStoreMarketingDTO;
import com.ecoolex.hotel.saas.pojo.EditStoreMarketingDTO;
import com.ecoolex.hotel.saas.ws.StoreMarketingService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

@GraphQL2Module(type = GraphQL2Module.Type.Platform, description = "商户首页营销管理")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
        key = StoreMarketingGraphql.AUTH,
        name = "商户首页营销管理",
        remark = "商户首页营销管理")
public class StoreMarketingGraphql {

    public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-STORE-MARKETING";

    private static final String AUTH_GET = AUTH + "-GET";

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    private static final String AUTH_ADD = AUTH + "-ADD";

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    private static final String AUTH_DEL = AUTH + "-DEL";

    @Reference
    private StoreMarketingService storeMarketingService;

    @QueryField(field = "storeMarketing", description = "商户首页营销详情")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商户首页营销详情")
    public StoreMarketing get(@NonNull Integer id, PlatformMemberAuthority user){
        user.assertAuth(AUTH_GET);
        return storeMarketingService.get(id);
    }

    @QueryField(field = "storeMarketingList", description =  "商户首页营销列表")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "商户首页营销列表")
    public Pagination<StoreMarketing> query(String name,@NonNull Integer storeId, @NonNull Page page, PlatformMemberAuthority user){
        user.assertAuth(AUTH_QUERY);
        return storeMarketingService.query(name,storeId,page);
    }

    @MutationField(field = "addStoreMarketing", description = "添加商户首页营销")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商户首页营销")
    public StoreMarketing add(@NonNull AddStoreMarketingDTO add, PlatformMemberAuthority user){
        user.assertAuth(AUTH_ADD);
        return storeMarketingService.add(add,user.userName());
    }

    @MutationField(field = "editStoreMarketing", description = "编辑商户首页营销")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商户首页营销")
    public StoreMarketing edit(@NonNull EditStoreMarketingDTO edit, PlatformMemberAuthority user){
        user.assertAuth(AUTH_EDIT);
        StoreMarketing old = storeMarketingService.get(edit.getId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商户首页营销不存在");
        BeanUtil.copy(edit, old);
        return storeMarketingService.edit(old, user.userName());
    }

    @MutationField(field = "delStoreMarketing", description = "删除商户首页营销")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商户首页营销")
    public Boolean delete(@NonNull Integer id, PlatformMemberAuthority user){
        user.assertAuth(AUTH_DEL);
        StoreMarketing old = storeMarketingService.get(id);
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商户首页营销不存在");
        storeMarketingService.delete(old, user.userName());
        return true;
    }
}
